package com.rk.cust.web;


import com.rk.cust.common.exception.ErrorCode;
import com.rk.cust.entity.Cust;
import com.rk.cust.entity.dto.request.AddCustRequestDto;
import com.rk.cust.entity.dto.request.ModifyCustRequestDto;
import com.rk.cust.entity.dto.response.CommonResponseDto;
import com.rk.cust.entity.dto.response.CustResponseDto;
import com.rk.cust.service.CustService;
import com.rk.cust.web.assembler.CustDtoAssembler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Pattern;

@RestController
@Slf4j
@Validated
public class CustApi {

    @Autowired
    private CustService custService;

    @Autowired
    private CustDtoAssembler custDtoAssembler;

    @GetMapping("/v1/custs/{id}")
    public CustResponseDto queryCust(@PathVariable @Pattern(regexp = "^\\d{13}$", message = "企业id为13位数字") @Valid String id) {
        log.info("查询id={}的企业信息", id);
        Cust cust = custService.queryCust(id);
        CustResponseDto custResponseDto = custDtoAssembler.assembler(cust);
        custResponseDto.setErrorCode(ErrorCode.SUCCESS.getErrorCode());
        custResponseDto.setErrorMsg(ErrorCode.SUCCESS.getErrorMsg());
        return custResponseDto;
    }


    @PostMapping("/v1/custs")
    public CommonResponseDto addCust(@RequestBody @Valid AddCustRequestDto addCustRequestDto) {
        log.info("新增企业:{}", addCustRequestDto);
        Cust cust = custDtoAssembler.assembler(addCustRequestDto);
        custService.addCust(cust);
        CommonResponseDto commonResponseDto = new CommonResponseDto();
        commonResponseDto.setErrorCode(ErrorCode.SUCCESS.getErrorCode());
        commonResponseDto.setErrorMsg(ErrorCode.SUCCESS.getErrorMsg());
        return commonResponseDto;
    }

    @PutMapping("/v1/custs/{id}")
    public CommonResponseDto modifyCust(@RequestBody @Valid ModifyCustRequestDto modifyCustRequestDto,
                                        @PathVariable @Pattern(regexp = "^\\d{13}$", message = "企业id为13位数字") @Valid String id) {
        Cust cust = custDtoAssembler.assembler(modifyCustRequestDto);
        cust.setId(Long.valueOf(id));
        custService.update(cust);
        CommonResponseDto commonResponseDto = new CommonResponseDto();
        commonResponseDto.setErrorCode(ErrorCode.SUCCESS.getErrorCode());
        commonResponseDto.setErrorMsg(ErrorCode.SUCCESS.getErrorMsg());
        return commonResponseDto;
    }

    @PatchMapping("/v1/activeCusts/{id}")
    public CommonResponseDto activeCust(@PathVariable @Pattern(regexp = "^\\d{13}$", message = "企业id为13位数字") @Valid String id) {
        log.info("激活企业 id={}", id);
        custService.activeCust(id);
        CommonResponseDto commonResponseDto = new CommonResponseDto();
        commonResponseDto.setErrorCode(ErrorCode.SUCCESS.getErrorCode());
        commonResponseDto.setErrorMsg(ErrorCode.SUCCESS.getErrorMsg());
        return commonResponseDto;
    }


}
